class Bucket < ActiveRecord::Base
  has_many :setting_usages
  has_many :settings, :through => :setting_usages, :order => 'name asc'

	validates_presence_of :name

  def new_setting_name
    ""
  end

  def new_setting_name=(setting_name)
    return if setting_name.nil? or setting_name.empty?
		return unless setting_exists?(setting_name)
		return if has_setting_with_name?(setting_name)

    setting_usages.build(:setting => Setting.find_by_name(setting_name))
  end

  def resolve_settings_for(axes)
    result = []
    settings.each do |s|
      result << s.resolve_for(axes)
    end
    return result
  end

	private

	def setting_exists?(setting_name)
    return !Setting.find_by_name(setting_name).nil?
	end

	def has_setting_with_name?(setting_name)
		settings.select { |s| s.name == setting_name }.size > 0
	end

end
